allocate larger chunks in constructors
authorØyvind Kolås <ok@src.gnome.org>
Tue, 16 Aug 2005 16:14:36 +0000 (16:14 +0000)
committerØyvind Kolås <ok@src.gnome.org>
Tue, 16 Aug 2005 16:14:36 +0000 (16:14 +0000)
ChangeLog
babl/babl-component.c
babl/babl-conversion.c
babl/babl-fish.c
babl/babl-format.c
babl/babl-image.c
babl/babl-model.c
babl/babl-pixel-format.c
babl/babl-sanity.c
babl/babl-type.c

index effa0f6419fd807cc056e3a68a89558e11c29f32..e2cb9b93c63cfc950d392dd8aed22f51604ff229 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2005-08-16  Øyvind Kolås  <pippin@gimp.org>
+
+       * babl/babl-component.c: (component_new, each_babl_component_destroy) allocate
+       larger chunk, (babl_component_new) removed Babl* switch, 
+       * babl/babl-conversion.c: (conversion_new): removed superflous runtime
+       warnings.
+       * babl/babl-fish.c: (babl_fish_process): reindent, added assert for
+       instance.
+       * babl/babl-image.c: (image_new, babl_image_new): reindent,
+       , (babl_image_new_from_linear): added asserts for parameters.
+       * babl/babl-model.c: (each_babl_model_destroy, model_new): allocate
+       larger chunk.
+       * babl/babl-pixel-format.c: (each_babl_pixel_format_destroy,
+       pixel_format_new): allocate larger chunk.
+       * babl/babl-sanity.c: (babl_conversion_source): removed FIXME comment
+       used for helper function just used to make code more readable.
+       * babl/babl-type.c: (each_babl_type_destroy, type_new): allocate
+       larger chunk.
+
 2005-08-16  Øyvind Kolås  <pippin@gimp.org>
 
        * babl/babl-classes.h: (BablSampling) added static name.
index 4f5d6939647a57f9c934b50cd466e4f5f472b248..6817bddd49fd19d90db9d16d62ed3c5bad57e14b 100644 (file)
@@ -30,7 +30,6 @@ each_babl_component_destroy (Babl *babl,
 {
   babl_free (babl->component.from);
   babl_free (babl->component.to);
-  babl_free (babl->instance.name);
   babl_free (babl);
   return 0;  /* continue iterating */
 }
@@ -44,10 +43,13 @@ component_new (const char *name,
 {
   Babl *babl;
 
-  babl                   = babl_calloc (sizeof (BablComponent), 1);
+  babl                   = babl_calloc (sizeof (BablComponent) +
+                                        strlen (name) + 1, 1);
+  babl->instance.name    = (void *) babl + sizeof (BablComponent);
+  strcpy (babl->instance.name, name);
+
   babl->class_type       = BABL_COMPONENT;
   babl->instance.id      = id;
-  babl->instance.name    = babl_strdup (name);
   babl->component.luma   = luma;
   babl->component.chroma = chroma;
   babl->component.alpha  = alpha;
@@ -59,7 +61,7 @@ Babl *
 babl_component_new (const char *name,
                     ...)
 {
-  va_list varg;
+  va_list     varg;
   Babl       *babl;
   int         id         = 0;
   int         luma    = 0;
@@ -79,32 +81,11 @@ babl_component_new (const char *name,
         {
           Babl *babl = (Babl*)arg;
 
-          switch (babl->class_type)
-            {
-              case BABL_TYPE:
-              case BABL_INSTANCE:
-              case BABL_COMPONENT:
-              case BABL_PIXEL_FORMAT:
-              case BABL_MODEL:
-              case BABL_SAMPLING:
-
-              case BABL_CONVERSION:
-              case BABL_CONVERSION_TYPE:
-              case BABL_CONVERSION_TYPE_PLANAR:
-              case BABL_CONVERSION_MODEL_PLANAR:
-              case BABL_CONVERSION_PIXEL_FORMAT:
-              case BABL_CONVERSION_PIXEL_FORMAT_PLANAR:
-              case BABL_FISH:
-              case BABL_FISH_REFERENCE:
-              case BABL_IMAGE:
-                babl_log ("%s(): %s unexpected",
-                          __FUNCTION__, babl_class_name (babl->class_type));
-                break;
-              case BABL_SKY: /* shut up compiler */
-                break;
-            }
+          babl_log ("%s(): %s unexpected",
+                    __FUNCTION__, babl_class_name (babl->class_type));
         }
       /* if we didn't point to a babl, we assume arguments to be strings */
+
       else if (!strcmp (arg, "id"))
         {
           id = va_arg (varg, int);
@@ -120,7 +101,6 @@ babl_component_new (const char *name,
           chroma = 1;
         }
 
-
       else if (!strcmp (arg, "alpha"))
         {
           alpha = 1;
index 9aa7f8702f5585d1fddc7a0672c033f70c83a794..2388b11318be7142726b831966c7d0074490a200 100644 (file)
@@ -67,14 +67,6 @@ conversion_new (const char        *name,
                       __FUNCTION__);
           }
         break;
-      case BABL_SAMPLING:
-        babl_log ("%s(): sampling conversions not implemented yet\n",
-                  __FUNCTION__);
-        break;
-      case BABL_COMPONENT:
-        babl_log ("%s(): component conversions do not make sense (except perhaps for gamma correction)\n",
-                  __FUNCTION__);
-        break;
       case BABL_MODEL:
         if (linear)
           {
@@ -93,7 +85,6 @@ conversion_new (const char        *name,
                       __FUNCTION__);
           }
         break;
-
       case BABL_PIXEL_FORMAT:
         if (linear)
           {
@@ -114,6 +105,8 @@ conversion_new (const char        *name,
           }
         break;
       default:
+          babl_log ("%s(): %s unexpected",
+                    __FUNCTION__, babl_class_name (babl->class_type));
         break;
     }
   if (!babl)
index 76f9e8d93ada41788ba21d73f0cdb81ec663bf05..403bb91666dfd93ef6ecf72338e98141250f1d7f 100644 (file)
@@ -191,24 +191,24 @@ void *fooC;
 #define BABL_MAX_BANDS   32
 
 int
-babl_fish_process (BablFish *babl_fish,
-                   void     *source,
-                   void     *destination,
-                   int       n)
+babl_fish_process (Babl *babl,
+                   void *source,
+                   void *destination,
+                   int   n)
 {
-  Babl *babl;
   Babl *imageA;
   Babl *imageB;
   Babl *imageC;
 
-  fooA = babl_malloc(sizeof (double) * n * 4); 
-  fooB = babl_malloc(sizeof (double) * n * 4); 
+  fooA = babl_malloc(sizeof (double) * n * 4);
+  fooB = babl_malloc(sizeof (double) * n * 4);
 
-  assert (babl_fish);
+  assert (babl);
   assert (source);
   assert (destination);
+  assert (babl->class_type == BABL_FISH ||
+          babl->class_type == BABL_FISH_REFERENCE);
 
-  babl = (Babl *)babl_fish;
   if (BABL_IS_BABL (source) ||
       BABL_IS_BABL (destination))
     {
@@ -217,10 +217,10 @@ babl_fish_process (BablFish *babl_fish,
       return -1;
     }
   
-  ((BablConversion*)(babl->reference_fish.type_to_double))->function.linear(
+  babl->reference_fish.type_to_double->function.linear(
           source,
           fooA,
-          n*  ((BablPixelFormat*)(babl_fish->source))->bands
+          n*  BABL(babl->fish.source)->pixel_format.bands
           );
 
   /* calculate planar representation of fooA, and fooB */
@@ -229,7 +229,7 @@ babl_fish_process (BablFish *babl_fish,
   imageB = babl_image_new_from_linear (fooB, babl_model_id (BABL_RGBA));
   /* transform fooA into fooB fooB is rgba double */
 
-  ((BablConversion*)(babl->reference_fish.model_to_rgba))->function.planar(
+  babl->reference_fish.model_to_rgba->function.planar(
           imageA->image.bands, 
           imageA->image.data,
           imageA->image.pitch,
@@ -246,7 +246,7 @@ babl_fish_process (BablFish *babl_fish,
   imageB = babl_image_new_from_linear (fooB, babl_model_id (BABL_RGBA));
   imageC = babl_image_new_from_linear (fooA, BABL(BABL((babl->fish.destination))->pixel_format.model[0]));
 
-  ((BablConversion*)(babl->reference_fish.rgba_to_model))->function.planar(
+  babl->reference_fish.rgba_to_model->function.planar(
           imageB->image.bands, 
           imageB->image.data,
           imageB->image.pitch,
@@ -256,9 +256,8 @@ babl_fish_process (BablFish *babl_fish,
           n);
 
 
-  ((BablConversion*)(babl->reference_fish.double_to_type))->function.linear(
-          fooA, destination, n * ((BablPixelFormat*)(babl_fish->destination))->bands
-          );
+  babl->reference_fish.double_to_type->function.linear(
+          fooA, destination, n * BABL(babl->fish.destination)->pixel_format.bands);
 
   babl_free (imageB);
   babl_free (imageC);
@@ -268,7 +267,6 @@ babl_fish_process (BablFish *babl_fish,
   return 0;
 }
 
-/*BABL_CLASS_TEMPLATE(BablFish, babl_fish, "BablFish")*/
-BABL_DEFINE_INIT(babl_fish)
-BABL_DEFINE_DESTROY(babl_fish)
-BABL_DEFINE_EACH(babl_fish)
+BABL_DEFINE_INIT    (babl_fish)
+BABL_DEFINE_DESTROY (babl_fish)
+BABL_DEFINE_EACH    (babl_fish)
index 14c60c2b3ddb394240ef8d4f6961c1f00fa5eaf5..f058e54e098d7772d1f25602c1576089e0b838a9 100644 (file)
@@ -34,11 +34,6 @@ each_babl_pixel_format_destroy (Babl *babl,
 {
   babl_free (babl->pixel_format.from);
   babl_free (babl->pixel_format.to);
-  babl_free (babl->pixel_format.component);
-  babl_free (babl->pixel_format.type);
-  babl_free (babl->pixel_format.sampling);
-  babl_free (babl->pixel_format.model);
-  babl_free (babl->instance.name);
   babl_free (babl);
 
   return 0;  /* continue iterating */
@@ -57,20 +52,29 @@ pixel_format_new (const char     *name,
   Babl *babl;
   int              band;
 
-  babl                     = babl_calloc (sizeof (BablPixelFormat), 1);
-
+  /* allocate all memory in one chunk */
+  babl  = babl_calloc (sizeof (BablPixelFormat) +
+                       strlen (name) + 1 +
+                       sizeof (BablModel*)     * (bands+1) +
+                       sizeof (BablComponent*) * (bands+1) +
+                       sizeof (BablSampling*)  * (bands+1) +
+                       sizeof (BablType*)      * (bands+1) +
+                       sizeof (int)            * (bands+1) +
+                       sizeof (int)            * (bands+1),1);
+
+  babl->instance.name          = ((void *)babl)                         + sizeof (BablPixelFormat);
+  babl->pixel_format.model     = ((void *)babl->instance.name)          + strlen (name) + 1;
+  babl->pixel_format.component = ((void *)babl->pixel_format.model)     + sizeof (BablModel*) * (bands+1);
+  babl->pixel_format.type      = ((void *)babl->pixel_format.component) + sizeof (BablComponent*) * (bands+1);
+  babl->pixel_format.sampling  = ((void *)babl->pixel_format.type)      + sizeof (BablType*) * (bands+1);
+  
   babl->class_type    = BABL_PIXEL_FORMAT;
   babl->instance.id   = id;
-  babl->instance.name = babl_strdup (name);
+  strcpy (babl->instance.name, name);
 
   babl->pixel_format.bands    = bands;
   babl->pixel_format.planar   = planar;
 
-  babl->pixel_format.model     = babl_malloc (sizeof (BablModel*)     * (bands+1));
-  babl->pixel_format.component = babl_malloc (sizeof (BablComponent*) * (bands+1));
-  babl->pixel_format.type      = babl_malloc (sizeof (BablType*)      * (bands+1));
-  babl->pixel_format.sampling  = babl_malloc (sizeof (BablSampling*)  * (bands+1));
-
   for (band=0; band < bands; band++)
     {
       babl->pixel_format.model[band] = model[band];
index 8dc132daca4b9f7096970619847e9f692ca97430..da204157160d08620731643d2d571e1d1e5f8ce6 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <string.h>
 #include <stdarg.h>
+#include <assert.h>
 
 #include "babl-internal.h"
 #include "babl-image.h"
@@ -48,15 +49,14 @@ image_new (int             bands,
            int            *stride)
 {
   Babl *babl;
-  int        band;
-
-  babl                = babl_calloc (
-                           sizeof (BablImage) +
-                           sizeof (BablComponent*) * (bands+1) +
-                           sizeof (void*)          * (bands+1) +
-                           sizeof (int)            * (bands+1) +
-                           sizeof (int)            * (bands+1),1);
-
+  int   band;
+
+  /* allocate all memory in one chunk */
+  babl  = babl_calloc (sizeof (BablImage) +
+                       sizeof (BablComponent*) * (bands+1) +
+                       sizeof (void*)          * (bands+1) +
+                       sizeof (int)            * (bands+1) +
+                       sizeof (int)            * (bands+1),1);
   babl->image.component     = ((void *)babl)                  + sizeof (BablImage);
   babl->image.data          = ((void *)babl->image.component) + sizeof (BablComponent*) * (bands+1);
   babl->image.pitch         = ((void *)babl->image.data)      + sizeof (void*)          * (bands+1);
@@ -97,6 +97,10 @@ babl_image_new_from_linear (void  *buffer,
 
   int            offset=0;
   int            calc_pitch=0;
+
+  assert (format);
+  assert (format->class_type == BABL_PIXEL_FORMAT ||
+          format->class_type == BABL_MODEL);
  
   switch (format->class_type)
     {
@@ -136,7 +140,6 @@ babl_image_new_from_linear (void  *buffer,
           }
         break;
       default:
-        babl_log ("%s(): Eeek!", __FUNCTION__);
         break;
     }
 
@@ -149,7 +152,7 @@ babl_image_new (void *first,
                 ...)
 {
   va_list        varg;
-  Babl           *babl;
+  Babl          *babl;
   int            bands     = 0;
   BablComponent *component [BABL_MAX_BANDS];
   void          *data      [BABL_MAX_BANDS];
index 0218090cb91d4bec8c35459400e6e2c1b2ab04b0..bfbd11dc4f567594eb1945f820798b9220d77e99 100644 (file)
@@ -29,8 +29,6 @@ each_babl_model_destroy (Babl *babl,
 {
   babl_free (babl->model.from);
   babl_free (babl->model.to);
-  babl_free (babl->model.component);
-  babl_free (babl->instance.name);
   babl_free (babl);
   return 0;  /* continue iterating */
 }
@@ -47,13 +45,16 @@ model_new (const char     *name,
   Babl *babl;
   int   i; 
 
-  babl                     = babl_calloc (sizeof (BablModel), 1);
-
+  babl                   = babl_calloc (sizeof (BablModel) +
+                                        sizeof (BablComponent*) * (components+1) +
+                                        strlen (name) + 1, 1);
+  babl->instance.name   = ((void*)babl) + sizeof (BablModel);
+  babl->model.component = ((void*)babl->instance.name) + strlen (name) + 1;
+  
   babl->class_type       = BABL_MODEL;
   babl->instance.id      = id;
-  babl->instance.name    = babl_strdup (name);
   babl->model.components = components;
-  babl->model.component  = babl_malloc (sizeof (BablComponent*) * (components+1));
+  strcpy (babl->instance.name, name);
 
   for (i=0; i < components; i++)
     {
index 14c60c2b3ddb394240ef8d4f6961c1f00fa5eaf5..f058e54e098d7772d1f25602c1576089e0b838a9 100644 (file)
@@ -34,11 +34,6 @@ each_babl_pixel_format_destroy (Babl *babl,
 {
   babl_free (babl->pixel_format.from);
   babl_free (babl->pixel_format.to);
-  babl_free (babl->pixel_format.component);
-  babl_free (babl->pixel_format.type);
-  babl_free (babl->pixel_format.sampling);
-  babl_free (babl->pixel_format.model);
-  babl_free (babl->instance.name);
   babl_free (babl);
 
   return 0;  /* continue iterating */
@@ -57,20 +52,29 @@ pixel_format_new (const char     *name,
   Babl *babl;
   int              band;
 
-  babl                     = babl_calloc (sizeof (BablPixelFormat), 1);
-
+  /* allocate all memory in one chunk */
+  babl  = babl_calloc (sizeof (BablPixelFormat) +
+                       strlen (name) + 1 +
+                       sizeof (BablModel*)     * (bands+1) +
+                       sizeof (BablComponent*) * (bands+1) +
+                       sizeof (BablSampling*)  * (bands+1) +
+                       sizeof (BablType*)      * (bands+1) +
+                       sizeof (int)            * (bands+1) +
+                       sizeof (int)            * (bands+1),1);
+
+  babl->instance.name          = ((void *)babl)                         + sizeof (BablPixelFormat);
+  babl->pixel_format.model     = ((void *)babl->instance.name)          + strlen (name) + 1;
+  babl->pixel_format.component = ((void *)babl->pixel_format.model)     + sizeof (BablModel*) * (bands+1);
+  babl->pixel_format.type      = ((void *)babl->pixel_format.component) + sizeof (BablComponent*) * (bands+1);
+  babl->pixel_format.sampling  = ((void *)babl->pixel_format.type)      + sizeof (BablType*) * (bands+1);
+  
   babl->class_type    = BABL_PIXEL_FORMAT;
   babl->instance.id   = id;
-  babl->instance.name = babl_strdup (name);
+  strcpy (babl->instance.name, name);
 
   babl->pixel_format.bands    = bands;
   babl->pixel_format.planar   = planar;
 
-  babl->pixel_format.model     = babl_malloc (sizeof (BablModel*)     * (bands+1));
-  babl->pixel_format.component = babl_malloc (sizeof (BablComponent*) * (bands+1));
-  babl->pixel_format.type      = babl_malloc (sizeof (BablType*)      * (bands+1));
-  babl->pixel_format.sampling  = babl_malloc (sizeof (BablSampling*)  * (bands+1));
-
   for (band=0; band < bands; band++)
     {
       babl->pixel_format.model[band] = model[band];
index 271f84eaa150248bfbe0a17e607e371cd8f84c4c..a861fff75499aab86519ff0478247f8568352f2f 100644 (file)
@@ -165,11 +165,7 @@ babl_sanity (void)
 
 }
 
-
-
-/* FIXME: these should be moved to babl-conversion.c */
-
-static Babl *babl_conversion_source      (Babl *babl)
+static Babl *babl_conversion_source (Babl *babl)
 {
   return (Babl *)babl->conversion.source;
 }
index 15ec8511e07fbdca32964e9419977a74daa9d80a..0412cfa4dd5c7314c90ab72f131989fab0424a1b 100644 (file)
@@ -29,7 +29,6 @@ static int
 each_babl_type_destroy (Babl *babl,
                         void *data)
 {
-  babl_free (babl->instance.name);
   babl_free (babl->type.from);
   babl_free (babl->type.to);
   babl_free (babl);
@@ -47,10 +46,11 @@ type_new (const char  *name,
   assert (bits != 0);
   assert (bits % 8 == 0);
   
-  babl                = babl_calloc (sizeof (BablType), 1);
+  babl                = babl_calloc (sizeof (BablType) + strlen (name) + 1, 1);
+  babl->instance.name = (void*) babl + sizeof (BablType);
   babl->class_type    = BABL_TYPE;
   babl->instance.id   = id;
-  babl->instance.name = babl_strdup (name);
+  strcpy (babl->instance.name, name);
   babl->type.bits     = bits;
 
   return babl;